//
//  MineSettingViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineSettingViewController.h"
#import "MineSettingOneCell.h"
#import "MineSettingTwoCell.h"
#import "MineSettingThreeCell.h"
@interface MineSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MineSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)setupUI
{
    self.view.backgroundColor = RGB_VALUE(0xF5F5F5);
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = RGB_VALUE(0xDC0000);
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(Height_NavBar);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setHitEdgeInsets:UIEdgeInsetsMake(-15, -15, -15, -15)];
    [headerView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-14);
        make.left.offset(18);
        make.width.height.mas_equalTo(16);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"设置";
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-9);
        make.centerX.offset(0);
        make.width.mas_equalTo(135);
        make.height.mas_equalTo(24);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[MineSettingOneCell class] forCellReuseIdentifier:@"MineSettingOneCell"];
    [_tableView registerClass:[MineSettingTwoCell class] forCellReuseIdentifier:@"MineSettingTwoCell"];
    [_tableView registerClass:[MineSettingThreeCell class] forCellReuseIdentifier:@"MineSettingThreeCell"];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(headerView.mas_bottom).offset(0);
        make.height.mas_equalTo(264);
    }];

    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:JSS_MEDIUM_FONT_4];
    loginBtn.backgroundColor = RGB_VALUE(0xDC0000);
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(20);
        make.right.offset(0);
        make.left.offset(0);
        make.height.mas_equalTo(44);
    }];
}

- (void)backBtnClick
{
    [self pop];
}

// 退出登录
- (void)loginBtnClick
{
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypePost URLString:@"logout" parameters:@{} done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [self pop];
        [[DVUserManager shareManager] logout];
//        DVUserManager *manager = [DVUserManager shareManager];
//        [manager setRootViewController:manager.loginController];
        
//        LoginViewController *vc = [[LoginViewController alloc] init];
//        [self pushVc:vc];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 3 || indexPath.row == 5) {
        MineSettingOneCell *cell = [MineSettingOneCell cellWithTableView:tableView];
        if (indexPath.row == 0) {
            cell.titleLabel.text = @"关于我们";
        } else if (indexPath.row == 1) {
            cell.titleLabel.text = @"帮助与反馈";
        } else if (indexPath.row == 3) {
            cell.titleLabel.text = @"清除缓存";
            cell.contentLabel.text = [NSString stringWithFormat:@"%2.fMB",[self getCacheSize]];
        } else if (indexPath.row == 5) {
            cell.titleLabel.text = @"隐私政策与条款";
        }
        return cell;
    } else if (indexPath.row == 2 ){
        MineSettingThreeCell *cell = [MineSettingThreeCell cellWithTableView:tableView];
        cell.titleLabel.text = @"消息通知";
        return cell;
    } else if (indexPath.row == 4 ){
        MineSettingTwoCell *cell = [MineSettingTwoCell cellWithTableView:tableView];
        cell.titleLabel.text = @"版本信息";
        cell.contentLabel.text = [NSString stringWithFormat:@"V%@",[UIApplication sharedApplication].appVersion];
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [[SDImageCache sharedImageCache] clearMemory];
                [[NSFileManager defaultManager] removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject]error:nil];
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 设置文字
                    MineSettingOneCell *cell = (MineSettingOneCell *)[tableView cellForRowAtIndexPath:indexPath];
                    cell.contentLabel.text = @"0MB";
//                    [weakSelf.tableView reloadData];
                });
            });
        }];
    }
}

- (double)getCacheSize {
    SDImageCache *imageCache = [SDImageCache  sharedImageCache];
    NSUInteger fileSize = [imageCache totalDiskSize]; // 以字节为单位
    NSString *myCachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];
    NSFileManager *fileM = [NSFileManager defaultManager];
    NSDictionary *fileInfo = [fileM attributesOfItemAtPath:myCachePath error:nil];
    fileSize += fileInfo.fileSize;
    return fileSize/1024.0/1024.0;
}

@end
